/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.base.service.customsdeclarationinfo;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.base.dao.customsdeclarationinfo.CustomsDeclarationInfoDao;
import com.jeesite.modules.base.dao.customsdeclarationinfo.CustomsDeclarationInfoDetailDao;
import com.jeesite.modules.base.entity.customsdeclarationinfo.CustomsDeclarationInfo;
import com.jeesite.modules.base.entity.customsdeclarationinfo.CustomsDeclarationInfoDetail;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 报关资料Service
 *
 * @author LV
 * @version 2021-03-02
 */
@Service
@Transactional(readOnly = true)
public class CustomsDeclarationInfoService extends CrudService < CustomsDeclarationInfoDao, CustomsDeclarationInfo > {

    @Autowired
    private CustomsDeclarationInfoDetailDao customsDeclarationInfoDetailDao;
    @Autowired
    private CodeRuleService codeRuleService;

    /**
     * 获取单条数据
     *
     * @param customsDeclarationInfo
     * @return
     */
    @Override
    public CustomsDeclarationInfo get(CustomsDeclarationInfo customsDeclarationInfo) {
        CustomsDeclarationInfo entity = super.get(customsDeclarationInfo);
        if (entity != null) {
            CustomsDeclarationInfoDetail customsDeclarationInfoDetail = new CustomsDeclarationInfoDetail(entity);
            customsDeclarationInfoDetail.setStatus(CustomsDeclarationInfoDetail.STATUS_NORMAL);
            entity.setCustomsDeclarationInfoDetailList(customsDeclarationInfoDetailDao.findList(customsDeclarationInfoDetail));
        }
        return entity;
    }

    /**
     * 查询分页数据
     *
     * @param customsDeclarationInfo      查询条件
     * @param customsDeclarationInfo.page 分页对象
     * @return
     */
    @Override
    public Page < CustomsDeclarationInfo > findPage(CustomsDeclarationInfo customsDeclarationInfo) {
        return super.findPage(customsDeclarationInfo);
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param customsDeclarationInfo
     */
    @Override
    @Transactional(readOnly = false)
    public void save(CustomsDeclarationInfo customsDeclarationInfo) {
        if (customsDeclarationInfo.getIsNewRecord()) {
            customsDeclarationInfo.setCustomsDeclarationInfoId(codeRuleService.getCode("customsclearance"));
            customsDeclarationInfo.setCreateBy(UserUtils.getUser().getUserCode());
            customsDeclarationInfo.setUpdateBy(UserUtils.getUser().getUserCode());
        }else {
            customsDeclarationInfo.setUpdateBy(UserUtils.getUser().getUserCode());
        }
        super.save(customsDeclarationInfo);
        if (customsDeclarationInfo.getIsNewRecord()) {
            codeRuleService.updateCurrentNumber("customsclearance");
        }
        // 保存 CustomsDeclarationInfo子表
        for (CustomsDeclarationInfoDetail customsDeclarationInfoDetail : customsDeclarationInfo.getCustomsDeclarationInfoDetailList()) {
            if (!CustomsDeclarationInfoDetail.STATUS_DELETE.equals(customsDeclarationInfoDetail.getStatus())) {
                customsDeclarationInfoDetail.setCustomsDeclarationInfoId(customsDeclarationInfo);
                if (customsDeclarationInfoDetail.getIsNewRecord()) {
                    customsDeclarationInfoDetailDao.insert(customsDeclarationInfoDetail);
                } else {
                    customsDeclarationInfoDetailDao.update(customsDeclarationInfoDetail);
                }
            } else {
                customsDeclarationInfoDetailDao.delete(customsDeclarationInfoDetail);
            }
        }
    }

    /**
     * 更新状态
     *
     * @param customsDeclarationInfo
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(CustomsDeclarationInfo customsDeclarationInfo) {
        super.updateStatus(customsDeclarationInfo);
    }

    /**
     * 删除数据
     *
     * @param customsDeclarationInfo
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(CustomsDeclarationInfo customsDeclarationInfo) {
        super.delete(customsDeclarationInfo);
        CustomsDeclarationInfoDetail customsDeclarationInfoDetail = new CustomsDeclarationInfoDetail();
        customsDeclarationInfoDetail.setCustomsDeclarationInfoId(customsDeclarationInfo);
        customsDeclarationInfoDetailDao.deleteByEntity(customsDeclarationInfoDetail);
    }

    public CustomsDeclarationInfo getOneByID(String customsDeclarationInfoId) {
        CustomsDeclarationInfo where = new CustomsDeclarationInfo();
        where.setCustomsDeclarationInfoId(customsDeclarationInfoId);
        return dao.getByEntity(where);
    }
}